<?php

namespace app\api\controller;

use think\Request;
use think\Session;
use think\Cache;

use app\api\model\Users ;


class Register extends Baseic
{
    private $db;
    //　初始化函数
    public function __auto(){
       $this->db = new Users();
    }

     /**
     * 手机注册
     * @access public
     */
    public function phoneRegister(){
      $results  = [];
      $data     = input('post.');

      // 比对验证码
      if(!captcha_check($data['captcha'])){
         $this->user_msg = '验证码错误|';
         $this->restError('0006|输入的信息有误');
         return json($this->returnResponse());
      }

      //检查用户是否已存在
          
      //信息校验

      //存入数据库
      $data['token'] = md5($data['user_name'].'-'.time());
      if($this->db->addUser($data)){
        $this->user_msg = '注册完成';
        $this->results  = [
                            'user_id'   => $this->db->id,
                            'user_name' => $data['user_name'],
                            'mobile'    => $data['mobile'],
                            'token'     => $data['token'],
                          ];
        $this->restSuccess();
      }else{
        $this->user_msg = '信息写入失败';
        $this->restError('0006|服务器错误');
      } 
    
      return json($this->returnResponse());
    }

    /**
    * QQ互联登录注册
    * @access public
    */
    public function qqRegister(){
        
    }

    /**
    * 微信互联登录注册
    * @access public
    */
    public function wechatRegister(){
        
    }

    /**
    * 验证码
    * @access public
    */
    public function sendCaptcha(){
       $data = [
           'captcha_src'  => $_SERVER['HTTP_HOST'].captcha_src(),
       ];
       
       $this->results = $data;
       $this->restSuccess();
       return json($this->returnResponse());
    }

    /**
    * 微信互联登录注册
    * @access public
    */
    public function sendSMS(){
     $phone = input('post.phone');
    if (isset($phone)) {
     //Cache::set("$phone-code",Session::get('sms_code'),0);
     Session::set($phone."_sms_code",rand(1000,9999));
     $data =  $this->sendSms("86", $phone, "牛立科技短信验证　您的验证码为".Session::get('sms_code')."。如非本人操作，请忽略本短信。");
     $this->results = $data ;
     $this->restSuccess($msgCode = '0000|操作成功');
    }
     

    }

    /**
        * 去服务商取得短信 全部参数使用字符串即可
        * @access public
        */
    private function getSms($nationCode, $phoneNumber, $content) {
        //短信验证
        $url = "https://yun.tim.qq.com/v3/tlssmssvr/sendsms";
        $sdkappid = config('api.SMS_APP_ID');
        $appkey = config('api.SMS_APP_KEY');

        $wholeUrl = $url."?sdkappid=".$sdkappid."&random=".Session::get('sms_code');
        $tel = new \stdClass();
        $tel->nationcode = $nationCode;
        $tel->phone  = $phoneNumber;
        $jsondata = new \stdClass();
        $jsondata->tel = $tel;
        $jsondata->type = "0";
        $jsondata->msg = $content;
        $jsondata->sig = md5($appkey.$phoneNumber);
        $jsondata->extend = "";     // 根据需要添加，一般保持默认
        $jsondata->ext = Session::get('sms_code');        // 根据需要添加，一般保持默认
        $curlPost =json_encode($jsondata);
        // echo $curlPost;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $wholeUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec($ch);
        if($ret === false) {
            return  curl_error($ch);
        } else {
            curl_close($ch);
            unset($url);
            unset($sdkappid);
            unset($wholeUrl);
            unset($tel);
            unset($jsondata);
            unset($ch);
            $json = json_decode($ret);
            if($json === false) {
            return  $ret;
            } else {
            return  $json;
            }
        }
      
  }

    
}
